using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YayiMgr.Ability.CommonApp;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.DentistsApp;
using YayiMgr.Ability.Docking.DentistsApp.Dto;
using YayiMgr.Domain.BasicData;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.DentistsApp
{
    public class DentistsServicer:AbilityServicer, IDentistsServicer
    {
        public IRepository<Dentists> DentistsRepo { get; set; }

        public async Task<QueryListDto<DentistsDto>> GetAllDentists()
        {
            List<Dentists> dentists = await DentistsRepo.GetListAsync();
            List<DentistsDto> dtos = ModelMapper.Map<List<Dentists>,List<DentistsDto>>(dentists);
            
            QueryListDto<DentistsDto> query = QueryListDto<DentistsDto>.Create(dtos, dtos.Count, 1, dtos.Count);
            return query;
        }

        public async Task<QueryListDto<DentistsDto>> GetDentists(DentistsQueryDto dto)
        {
            IQueryable<Dentists> dentists = DentistsRepo.AsQueryable();
            if(dto.CompanyId!=null)
                dentists = dentists.Where(d=>d.CompanyId==dto.CompanyId);            
            if(!String.IsNullOrWhiteSpace(dto.Name))
                dentists = dentists.Where(d=>d.Name.Contains(dto.Name));
            if(!String.IsNullOrWhiteSpace(dto.Gender))
                dentists = dentists.Where(d=>d.Gender==dto.Gender);
            if(!String.IsNullOrWhiteSpace(dto.Post))
                dentists = dentists.Where(d=>d.Post.Contains(dto.Post));
            if(!String.IsNullOrWhiteSpace(dto.OfficePhone))
                dentists = dentists.Where(d=>d.OfficePhone.Contains(dto.OfficePhone));

            List<Dentists> items = await dentists.OrderBy(d=>d.CompanyId).ThenBy(d=>d.Gender).ThenBy(d=>d.Name)
                                        .Skip((dto.PageIndex-1)*dto.PageSize).Take(dto.PageSize).ToListAsync();
            List<DentistsDto> dtos = ModelMapper.Map<List<Dentists>, List<DentistsDto>>(items);
            int total = await dentists.CountAsync();
            QueryListDto<DentistsDto> query = QueryListDto<DentistsDto>.Create(dtos, total, dto.PageIndex, dto.PageSize);
            return query;
        }

        public async Task<ResponseDto<DentistsDto>> GetDentistsById(Guid id)
        {
            Dentists dentists = await DentistsRepo.GetAsync(id);
            if(dentists==null)
                return ResponseDto<DentistsDto>.CreateFail("没有数据");
            DentistsDto dentistsDtos = ModelMapper.Map<Dentists,DentistsDto>(dentists);
            return ResponseDto<DentistsDto>.CreateSuccess(dentistsDtos);
        }

        public async Task<ResponseDto<DentistsDto>> AddDentists(DentistsCreateDto input)
        {
            Dentists dentists = ModelMapper.Map<DentistsCreateDto,Dentists>(input);
            dentists = await DentistsRepo.InsertAsync(dentists);
            DentistsDto dentistsDtos = ModelMapper.Map<Dentists,DentistsDto>(dentists);
            return ResponseDto<DentistsDto>.CreateSuccess(dentistsDtos);
        }

        public async Task<ResponseDto<DentistsDto>> UpdateDentists(Guid id, DentistsUpdateDto input)
        {
            Dentists dentists = await DentistsRepo.GetAsync(id);
            if(dentists==null)
                return ResponseDto<DentistsDto>.CreateFail("没有数据");

            dentists.UpdateEntity<Dentists>(ModelMapper.Map<DentistsUpdateDto,Dentists>(input), new List<string>{"Id"});
            dentists = await DentistsRepo.UpdateAsync(dentists);
            DentistsDto dentistsDtos = ModelMapper.Map<Dentists,DentistsDto>(dentists);
            return ResponseDto<DentistsDto>.CreateSuccess(dentistsDtos);
        }
        
        public async Task DeleteDentists(Guid id)
        {
            await DentistsRepo.DeleteAsync(id);
        }
    }
}